﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="itemview.aspx.cs" Inherits="A_itemview" %>

<%@ Register Src="~/A/controls/UCItem.ascx" TagPrefix="UC" TagName="ItemInfo" %>
<%@ Register Src="~/A/controls/UCCategory.ascx" TagPrefix="UC" TagName="CategoryInfo" %>
<%@ Register Src="~/A/controls/UCRecommandCategory.ascx" TagPrefix="UC" TagName="RecommandCategory" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>条文管理</title>
    <link href="../css/jquery-ui-1.8.4.custom.css" rel="stylesheet" type="text/css" />
    <link href="../css/itemview.css" rel="stylesheet" type="text/css" />
    <link href="../css/jquery.treeview.css" rel="stylesheet" type="text/css" />

    <script src="../js_/jquery-1.4.2.min.js" type="text/javascript"></script>

    <script src="../js_/jquery-ui-1.8.5.custom.min.js" type="text/javascript"></script>

    <script src="../js_/jquery.treeview.min.js" type="text/javascript"></script>

    <script src="../js_/jquery.splittercontainer.js" type="text/javascript"></script>

    <script src="../js_/jquery.cascadeselector.js" type="text/javascript"></script>

    <script src="../js_/jquery.masklayer.js" type="text/javascript"></script>

    <script src="../js_/itemview.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $('#splitcontainer').splittercontainer({
                panel1: { target: $('#treecontainer') },
                panel2: { target: $('#maincontent') },
                panel1InitWidth: 200,
                splitterCssClass: 'splitter',
                fixedPanle: 'p1',
                orientation: 'h',
                enableSplitterDblClick: false
            });

            /* ItemList */
            $('#ItemList').parent().bind('dblclick', function(e) {
                var ele = e.srcElement || e.target;
                var target = $(ele);
                var data = null;
                if (target.is("td")) {
                    data = target.parent().data('item');
                } else if (target.is("tr")) {
                    data = target.data('item');
                } else {
                    return;
                }
                if (data) {
                    ModifyItem(data, function(succeed, data) {
                        if (succeed) {
                            if (data.CategoryId == $('#CurrentCategory').val())
                                RefreshItemUI(data);
                            else
                                DeleteItemsUI([data.ItemId]);
                            CheckSltAllItemState();
                        }
                    }, null);
                }
            });
            $('#ItemList').parent().bind('click', function(e) {
                var ele = e.srcElement || e.target;
                var target = $(ele);
                if (target.is("img")) {
                    switch (target.attr('tag')) {
                        case 'item_modify':
                            {
                                target.parent().trigger('dblclick');
                            } break;
                        case 'item_del':
                            {
                                var data = target.parent().parent().data('item');
                                if (!data || !data.ItemId) return;
                                if (!confirm('您确认删除条文“' + data.Title + '”吗？')) return;
                                $('#content').masklayer('show');
                                DeleteItems([data.ItemId],
                        function(idArr) {
                            DeleteItemsUI(idArr); $('#content').masklayer('hide');
                            CheckSltAllItemState();
                        },
                        function(err) {
                            alert('删除失败！错误信息：' + err);
                            $('#content').masklayer('hide');
                        });
                            }
                            break;
                        case 'slted_del':
                            {
                                var idArr = [];
                                $('#ItemList').find(':checkbox[checked]').each(function() {
                                    idArr.push($(this).parent().parent().data('item').ItemId);
                                });
                                if (idArr.length < 1)
                                    return;
                                if (!confirm('您确认删除选择的 ' + idArr.length + ' 项吗？'))
                                    return;
                                $('#content').masklayer('show');
                                DeleteItems(idArr,
                        function(idArr) {
                            DeleteItemsUI(idArr);
                            $('#content').masklayer('hide');
                            CheckSltAllItemState();
                        },
                        function(err) {
                            alert('删除失败！错误信息：' + err);
                            $('#content').masklayer('hide');
                        });
                            }
                            break;
                        default:
                            return;
                    }
                    e.stopPropagation();
                } else if (target.is('#sltAllItems')) {
                    var slted = target[0].checked;
                    $('#ItemList').find(":checkbox").each(function() {
                        this.checked = slted ? true : '';
                    });
                }
                else if (target.is(':checkbox')) {
                    if (!target[0].checked) {
                        $('#sltAllItems')[0].checked = '';
                    } else {
                        CheckSltAllItemState();
                    }
                }
            });
            /* UnchectedItemList */
            $('#UnchectedItemList').parent().bind('dblclick', function(e) {
                var ele = e.srcElement || e.target;
                var target = $(ele);
                var data = null;
                if (target.is("td")) {
                    data = target.parent().data('item');
                } else if (target.is("tr")) {
                    data = target.data('item');
                } else {
                    return;
                }
                if (data) {
                    ModifyItem(data, function(succeed, data) {
                        if (succeed) {
                            if (data.IsChecked) {
                                DeleteItemsUI([data.ItemId]);
                                if (data.CategoryId == $('#CurrentCategory').val()) {
                                    LoadItem($('#CurrentCategory').val());
                                }
                            } else {
                                RefreshItemUI(data);
                            }
                            CheckSltAllItemState();
                            CheckSltAllUnchkItemState();
                        }
                    }, null);
                }
            });
            $('#UnchectedItemList').parent().bind('click', function(e) {
                var ele = e.srcElement || e.target;
                var target = $(ele);
                if (target.is("img")) {
                    switch (target.attr('tag')) {
                        case 'item_modify':
                            target.parent().trigger('dblclick');
                            break;
                        case 'item_del':
                            {
                                var data = target.parent().parent().data('item');
                                if (!data || !data.ItemId) return;
                                if (!confirm('您确认删除条文“' + data.Title + '”吗？')) return;
                                $('#content').masklayer('show');
                                DeleteItems([data.ItemId],
                        function(idArr) {
                            DeleteItemsUI(idArr);
                            $('#content').masklayer('hide');
                            CheckSltAllUnchkItemState();
                        },
                        function(err) {
                            alert('删除失败！错误信息：' + err);
                            $('#content').masklayer('hide');
                        });
                            }
                            break;
                        case 'item_chk':
                            {
                                var data = target.parent().parent().data('item');
                                if (!data || !data.ItemId) return;
                                if (!confirm('您确认让条文“' + data.Title + '”通过审核吗？')) return;
                                $('#content').masklayer('show');
                                CheckItems([data.ItemId],
                        function(idArr) {
                            DeleteItemsUI(idArr);
                            if (data.CategoryId == $('#CurrentCategory').val()) {
                                LoadItem($('#CurrentCategory').val());
                            }
                            $('#content').masklayer('hide');
                            CheckSltAllUnchkItemState();
                        },
                        function(err) {
                            alert('操作失败！错误信息：' + err);
                            $('#content').masklayer('hide');
                        });
                            }
                            break;
                        case 'slted_chk':
                            {
                                var idArr = [];
                                var d = null;
                                var updateItem = false;
                                var curCategoryId = $('#CurrentCategory').val();
                                $('#UnchectedItemList').find(':checkbox[checked]').each(function() {
                                    d = $(this).parent().parent().data('item');
                                    idArr.push(d.ItemId);
                                    if (d.CategoryId == curCategoryId) {
                                        updateItem = true;
                                    }
                                });
                                if (idArr.length < 1)
                                    return;
                                if (!confirm('您确认让选择的 ' + idArr.length + ' 项通过审核吗？'))
                                    return;
                                $('#content').masklayer('show');
                                CheckItems(idArr,
                        function(idArr) {
                            DeleteItemsUI(idArr);
                            if (updateItem) LoadItem(curCategoryId);
                            $('#content').masklayer('hide');
                            CheckSltAllUnchkItemState();
                        },
                        function(err) {
                            alert('操作失败！错误信息：' + err);
                            $('#content').masklayer('hide');
                        });
                            }
                            break;
                        case 'slted_del':
                            {
                                var idArr = [];
                                $('#UnchectedItemList').find(':checkbox[checked]').each(function() {
                                    idArr.push($(this).parent().parent().data('item').ItemId);
                                });
                                if (idArr.length < 1)
                                    return;
                                if (!confirm('您确认删除选择的 ' + idArr.length + ' 项吗？'))
                                    return;
                                $('#content').masklayer('show');
                                DeleteItems(idArr,
                        function(idArr) {
                            DeleteItemsUI(idArr);
                            $('#content').masklayer('hide');
                            CheckSltAllUnchkItemState();
                        },
                        function(err) {
                            alert('删除失败！错误信息：' + err);
                            $('#content').masklayer('hide');
                        });
                            }
                            break;
                        default:
                            return;
                    }
                    e.stopPropagation();
                } else if (target.is('#sltAllUnchkItem')) {
                    var slted = target[0].checked;
                    $('#UnchectedItemList').find(":checkbox").each(function() {
                        this.checked = slted ? true : '';
                    });
                }
                else if (target.is(':checkbox')) {
                    if (!target[0].checked) {
                        $('#sltAllUnchkItem')[0].checked = '';
                    } else {
                        CheckSltAllUnchkItemState();
                    }
                }
            });
            /* Category */
            $('#CategoryList').sortable();
            $('#CategoryList').disableSelection();

            $('#CategoryList').bind('dblclick', function(e) {
                var ele = e.srcElement || e.target;
                var target = $(ele), data;
                if (target.is('ul'))
                    return;
                else if (target.is("li > img")) {
                    Jump2Node(target.parent().attr('catid'));
                }
            });
            $('#CategoryList').bind('click', function(e) {
                var ele = e.srcElement || e.target;
                var target = $(ele);
                if (target.is(':checkbox')) {
                    if (!ele.checked) {
                        $('#sltAllCategory')[0].checked = '';
                    } else {
                        CheckSltAllCatetoryState();
                    }
                } else if (target.is('img')) {
                    switch (target.attr('tag')) {
                        case 'folder_modify':
                            var data = target.parent().parent().data("cat");
                            if (data) {
                                ModifyCategory(data, function(succeed, d) {
                                    if (succeed) {
                                        if (d.ParentCategoryId == $('#CurrentCategory').val() && d.Order == data.Order) {
                                            $('#navtree').find("li[cid=" + d.CategoryId + "]").each(function() {
                                                $(this).find('a').html(d.CategoryName);
                                            });
                                            RefreshCategoryUI(d);
                                        } else {
                                            RefreshChildCategories($('#CurrentCategory').val());
                                        }
                                    }
                                });
                            }
                            break;
                        case 'folder_del':
                            var data = target.parent().parent().data("cat");
                            if (data && data.CategoryId) {
                                if (!confirm("您确认要删除 " + data.CategoryName + " 吗？")) return;
                                $('#content').masklayer('show');
                                DeleteCategory([data.CategoryId], function() {
                                    RefreshChildCategories(data.ParentCategoryId);
                                    $('#content').masklayer('hide');
                                }, function(err) {
                                    alert('删除失败，错误信息：' + err + '。');
                                    $('#content').masklayer('hide');
                                });
                            }
                            break;
                        default:
                            return;
                    }
                    e.stopPropagation();
                }
            });

            $('#tabs').show();
            $('#tabs').tabs({
                selected: 2,
                select: function(event, ui) {
                    if (ui.index == 3) {
                        $("input:button[value='加载数据']", ui.panel).trigger('click');
                    } else if (ui.index == 5) {
                        $("input:button[value='加载数据']", ui.panel).trigger('click');
                    }
                }
            });

            setTimeout(function() {
                BindTreeNode($('#navtree'), 0);
                $('#navtree').treeview({
                    animated: "fast",
                    collapsed: true,
                    toggle: function() {
                        var curLI = $(this);
                        if (curLI.attr('databind')) return;
                        var cId = curLI.attr('cid');
                        BindTreeNode(curLI, cId);
                        return;
                    }
                });
            }, 500);
            //RefreshUncheckedItems();
        });

        function ModifyCurrentCategory() {
            var data = $('#CurrentCategory').data('cat');
            if (data) {
                ModifyCategory(data, function(succeed, d) {
                    if (succeed) {
                        RefreshCategoryUI(d);
                        if (d.ParentCategoryId == data.ParentCategoryId) {
                            if (d.Order != data.Order) {
                                $('#navtree').find("li[cid=" + d.ParentCategoryId + "][databind]").each(function(index) {
                                    BindTreeNode($(this), cid);
                                });
                            }
                        } else if (data.ParentCategoryId == 0) {
                            BindTreeNode($('#navtree'), null, false);
                            Jump2Node(data.CategoryId);
                        } else {
                            $('#navtree').find("li[cid=" + data.ParentCategoryId + "][databind]").each(function(index) {
                                BindTreeNode($(this), data.ParentCategoryId, false);
                            });
                            $('#navtree').find("li[cid=" + d.ParentCategoryId + "][databind]").each(function(index) {
                                BindTreeNode($(this), d.ParentCategoryId, false);
                            });
                            Jump2Node(d.CategoryId);
                        }
                    }
                });
            }
        }

        function DeleteCurrentCategory() {
            var data = $('#CurrentCategory').data('cat');
            if (!data || !data.CategoryId) return;
            if (!confirm("您确认删除 " + data.CategoryName + " 吗？")) return;
            $('#content').masklayer('show');
            DeleteCategory([data.CategoryId], function() {
                if (data.ParentCategoryId > 0) {
                    $('#navtree').find("li[cid=" + data.ParentCategoryId + "]").each(function(index) {
                        BindTreeNode($(this), data.ParentCategoryId, false);
                    });
                    Jump2Node(data.ParentCategoryId);
                } else {
                    BindTreeNode($('#navtree'), null, false);
                    LoadCurrentCategory();
                }
                $('#content').masklayer('hide');
            }, function(msg) {
                alert('删除失败。错误信息：' + msg);
                $('#content').masklayer('hide');
            });
        }

        function DeleteSltedCategories() {
            var arr = [];
            var cid, cat;
            $('#CategoryList').find(':checkbox[checked]').each(function() {
                cat = $(this).parent().parent().data('cat');
                if (cat) {
                    cid = cat.CategoryId;
                    if (cid) arr.push(cid);
                }
            });
            if (arr.length == 0) return;
            if (!confirm("您确认删除选中的这 " + arr.length + " 项吗？")) return;
            $('#content').masklayer('show');
            DeleteCategory(arr, function() {
                RefreshChildCategories($('#CurrentCategory').val());
                $('#content').masklayer('hide');
            }, function(err) {
                alert('删除失败，错误信息：' + err + '。');
                $('#content').masklayer('hide');
            });
        }

        function CheckSltAllItemState() {
            if ($('#ItemList').find(':checkbox').length == 0) {
                $('#sltAllItems')[0].checked = '';
            }
            else if ($('#ItemList').find(':checkbox[checked]').length == $('#ItemList').find(':checkbox').length) {
                $('#sltAllItems')[0].checked = true;
            } else {
                $('#sltAllItems')[0].checked = '';
            }
        }

        function CheckSltAllUnchkItemState() {
            if ($('#UnchectedItemList').find(':checkbox').length == 0) {
                $('#sltAllUnchkItem')[0].checked = '';
            }
            else if ($('#UnchectedItemList').find(':checkbox[checked]').length == $('#UnchectedItemList').find(':checkbox').length) {
                $('#sltAllUnchkItem')[0].checked = true;
            } else {
                $('#sltAllUnchkItem')[0].checked = '';
            }
        }

        function ShowAddItem() {
            var cid = $('#CurrentCategory').val();
            if (!cid) return;
            AddItem(cid, function(data) {
                if (data.CategoryId == cid) {
                    LoadItem(cid);
                    CheckSltAllItemState();
                }
            }, null);
        }

        function ShowBatchAddItem() {
            var cid = $('#CurrentCategory').val();
            if (!cid) return;
            BatchAddItem(cid, function(catId) {
                if (catId == cid) {
                    LoadItem(cid);
                    CheckSltAllItemState();
                }
            }, null);
        }

        function ShowAddCategory() {
            var cid = $('#CurrentCategory').val();
            if (!cid) return;
            AddCategory(cid, function(data) {
                if (data.ParentCategoryId == cid)
                    RefreshChildCategories(cid);
            }, null);
        }

        function ShowBatAddCategory() {
            var cid = $('#CurrentCategory').val();
            if (!cid) return;
            BatchAddCategory(cid, function(pid) {
                if (pid == cid)
                    RefreshChildCategories(cid);
            }, null);
        }

        function RefreshChildCategories(cid) {
            $('#navtree').find("li[cid=" + cid + "][databind]").each(function(index) {
                BindTreeNode($(this), cid);
            });
            LoadCategory(cid);
        }

        function SelectAllCategory() {
            var slted = $('#sltAllCategory')[0].checked;
            $('#CategoryList').find(':checkbox').each(function() {
                this.checked = slted ? true : '';
            });
        }

        function CheckSltAllCatetoryState() {
            if ($('#CategoryList').find(':checkbox').length == 0) {
                $('#sltAllCategory')[0].checked = '';
            } else if ($('#CategoryList').find(':checkbox').length == $('#CategoryList').find(':checkbox[checked]').length) {
                $('#sltAllCategory')[0].checked = true;
            } else {
                $('#sltAllCategory')[0].checked = '';
            }
        }
    </script>

</head>
<body>
    <form id="form1" runat="server" onsubmit="return false;">
    <div>
        <div id="header">
        </div>
        <div id="content">
            <div id="splitcontainer">
                <div id="treecontainer">
                    <ul id="navtree">
                        <li>目录</li>
                    </ul>
                </div>
                <div id="maincontent">
                    <div id="tabs" style="display: none;">
                        <ul>
                            <li><a href="#tab-item">条文管理</a></li>
                            <li><a href="#tab-category">目录管理</a></li>
                            <li><a href="#tab-search" title='仅查找当前目录及其子目录'>快速查找</a></li>
                            <li><a href="#tab-uncheckeditem">未审核条文</a></li>
                            <DirectoryControl:RolePanel ID="rpRootCategory1" runat="server">
                                <li><a href="#tab-rootcategory">添加根目录</a></li>
                                <li><a href="#tab-recommandcategory">推荐目录管理</a></li>
                            </DirectoryControl:RolePanel>
                        </ul>
                        <div id="tab-item">
                            <button onclick="LoadItem($('#CurrentCategory').val());">
                                刷新数据</button>
                            <button onclick='ShowAddItem();'>
                                添加条文</button>
                            <button onclick='ShowBatchAddItem();'>
                                批量添加</button>
                            <hr />
                            <table>
                                <colgroup>
                                    <col width="" />
                                    <col width="100px" />
                                    <col width="" />
                                    <col width="" />
                                    <col width="100px" />
                                    <col width="180px" />
                                </colgroup>
                                <thead>
                                    <tr>
                                        <th>
                                            <input type="checkbox" id='sltAllItems' />
                                            <img tag='slted_del' title='删除所选' src='../css/images/delete.png' />
                                        </th>
                                        <th>
                                            Logo
                                        </th>
                                        <th>
                                            标题
                                        </th>
                                        <th>
                                            链接
                                        </th>
                                        <th>
                                            作者
                                        </th>
                                        <th>
                                            添加日期
                                        </th>
                                    </tr>
                                </thead>
                                <tbody id="ItemList">
                                </tbody>
                            </table>
                        </div>
                        <div id="tab-category">
                            <input id='sltAllCategory' type="checkbox" onchange="SelectAllCategory();" />全选
                            <button onclick="RefreshChildCategories($('#CurrentCategory').val());">
                                刷新数据</button>
                            <button onclick="DeleteSltedCategories();">
                                删除所选</button>
                            <button onclick="ShowAddCategory();">
                                添加子目录</button>
                            <button onclick="ShowBatAddCategory();">
                                批量添加</button>
                            <button onclick="ModifyCurrentCategory();">
                                编辑当前目录</button>
                            <button onclick="DeleteCurrentCategory();">
                                删除当前目录</button>
                            <hr />
                            <ul id="CategoryList">
                            </ul>
                        </div>
                        <div id="tab-search">
                            <div>
                                <label>
                                    关键字：</label>
                                <input type="text" id="btKeyword" />
                                <label>
                                    搜索</label>
                                <select>
                                    <optgroup>
                                        <option selected="selected" value="条文">条文</option>
                                        <option value="分类">目录</option>
                                    </optgroup>
                                </select>
                                <input type="button" value="搜 索" />
                            </div>
                            <hr />
                        </div>
                        <div id="tab-uncheckeditem">
                            <div>
                                <label>
                                    所有目录下未审核条文</label>
                                <input type="button" value="加载数据" onclick="this.value='刷新数据';RefreshUncheckedItems()" />
                            </div>
                            <hr />
                            <table>
                                <colgroup>
                                    <col width="" />
                                    <col width="100px" />
                                    <col width="" />
                                    <col width="" />
                                    <col width="100px" />
                                    <col width="180px" />
                                </colgroup>
                                <thead>
                                    <tr>
                                        <th>
                                            <input id="sltAllUnchkItem" type="checkbox" />
                                            <img tag='slted_chk' title='设置选择项通过审核' src='../css/images/check.png' />
                                            <img tag='slted_del' title='删除所选' src='../css/images/delete.png' />
                                        </th>
                                        <th>
                                            Logo
                                        </th>
                                        <th>
                                            标题
                                        </th>
                                        <th>
                                            链接
                                        </th>
                                        <th>
                                            作者
                                        </th>
                                        <th>
                                            添加日期
                                        </th>
                                    </tr>
                                </thead>
                                <tbody id="UnchectedItemList">
                                </tbody>
                            </table>
                        </div>
                        <DirectoryControl:RolePanel ID="rpRootCategory2" runat="server">
                            <div id='tab-rootcategory'>
                                <center>
                                    <table style="text-align: left;">
                                        <tr>
                                            <td>
                                                目录名：
                                            </td>
                                            <td>
                                                <input type="text" id="roocat_tbName" style="width: 320px;" />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                序号：
                                            </td>
                                            <td>
                                                <input type="text" id="roocat_tbOrder" style="width: 40px;" />【从1开始，非正数时自动添加到最后】
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2" style="text-align: right">
                                                <button onclick="AddRootCategory();">
                                                    添 加</button>
                                                <button onclick="ResetRootCategory();">
                                                    清 空</button>
                                            </td>
                                        </tr>
                                    </table>
                                </center>
                            </div>
                            <div id='tab-recommandcategory'>
                                <input id="sltAllRecommandCat" type="checkbox" onchange="SelectAllRecommandCat();" />全选
                                <input type="button" value="取消选中项推荐状态" onclick="UnRecommandSltedCat();" />
                                <input type="button" value="添加推荐目录" onclick="RecommandCategory.AddRecommandCateogry()" />
                                <input type="button" value="加载数据" onclick="this.value='刷新数据';$('#sltAllRecommandCat')[0].checked = '';RefreshRecommandCategory();" />
                                <hr />
                                <ul id="RecommandCategoryList">
                                </ul>
                            </div>

                            <script type="text/javascript" src="../js_/itemview-rootcategoryadmin.js"></script>

                        </DirectoryControl:RolePanel>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div style="display: none;">
        <input type="hidden" id="CurrentCategory" />
        <UC:ItemInfo ID="ItemInfo1" runat="server" />
        <UC:CategoryInfo ID="CategoryInfo1" runat="server" />
        <DirectoryControl:RolePanel ID="rpRootCategory3" runat="server">
            <UC:RecommandCategory ID='RecommandCategory1' runat="server" />
        </DirectoryControl:RolePanel>
    </div>
    </form>
</body>
</html>
